AB20 走迷宫
import java.util.;

 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
         注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) {  注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            int x1 = in.nextInt() - 1;
            int y1 = in.nextInt() - 1;
            int x2 = in.nextInt() - 1;
            int y2 = in.nextInt() - 1;
            char[][] arr = new char[n][m];
            boolean[][] flag = new boolean[n][m];

            for (int i = 0; i  n; i++) {
                arr[i] = in.next().toCharArray();
            }
            QueueData queue = new LinkedList();
            queue.add(new Data(x1, y1, 0));
            flag[x1][y1] = true;
            while (!queue.isEmpty()) {
                Data tmp = queue.poll();
                int x = tmp.x;
                int y = tmp.y;
                int count = tmp.count;
                x++;
                if (x  n && arr[x][y] == '.' && flag[x][y] == false) {
                    if (x == x2 && y == y2) {
                        System.out.println(count + 1);
                        return;
                    }
                    flag[x][y] = true;
                    queue.add(new Data(x, y, count + 1));
                }
                x--;
                x--;
                if (x = 0 && arr[x][y] == '.' && flag[x][y] == false) {
                    if (x == x2 && y == y2) {
                        System.out.println(count + 1);
                        return;
                    }
                    flag[x][y] = true;
                    queue.add(new Data(x, y, count + 1));
                }
                x++;
                y--;
                if (y = 0 && arr[x][y] == '.' && flag[x][y] == false) {
                    if (x == x2 && y == y2) {
                        System.out.println(count + 1);
                        return;
                    }
                    flag[x][y] = true;
                    queue.add(new Data(x, y, count + 1));
                }
                y++;
                y++;
                if (y  m && arr[x][y] == '.' && flag[x][y] == false) {
                    if (x == x2 && y == y2) {
                        System.out.println(count + 1);
                        return;
                    }
                    flag[x][y] = true;
                    queue.add(new Data(x, y, count + 1));
                }
            }
            System.out.println(-1);
        }
    }

    static class Data {
        int x;
        int y;
        int count;
        public Data(int x, int y, int count) {
            this.x = x;
            this.y = y;
            this.count = count;
        }
    }
}